//
//  NotifyLabel.h
//  sheyingzu
//
//  Created by tyu on 29.5.24.
//

#import "NotifyLabel.h"

@implementation NotifyLabel


+(void)NotifyWithString:(NSString *)string{
    

//    UIWindow * window = [UIApplication sharedApplication].windows[0];
    UIWindow * window =  [ZXZUtil getKeyWindow];

   
    UIViewController *rootVC = window.rootViewController;
    

    NotifyLabel *Label=[NotifyLabel new];
    
    
    NSInteger y1=(ScreenHeight-60)/2;
    NSInteger w1=[Label calculatedwidthString:string]+50;
    NSInteger x1=(ScreenWidth-w1)/2;
    NSInteger h1=60;
    Label.frame = CGRectMake(x1,y1,w1, h1);
    Label.backgroundColor=[UIColor colorWithRed:59/255.0 green:59/255.0 blue:59/255.0 alpha:1];
    Label.alpha = 0.8;
    Label.layer.cornerRadius=10;
    Label.layer.masksToBounds=YES;
    Label.textColor=[UIColor whiteColor];
    Label.text=string;
    Label.textAlignment=NSTextAlignmentCenter;
    Label.font=[UIFont systemFontOfSize:18 weight:0];
    [rootVC.view addSubview:Label];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.3 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
        [UIView animateWithDuration:0.5f animations:^{
            Label.alpha = 0;
        } completion:^(BOOL finished) {
            [Label removeFromSuperview];
        }];
        
        });
    }

-(NSInteger)calculatedwidthString:(NSString *)string{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    //[paragraphStyle setLineSpacing:4];
    CGSize size = [string boundingRectWithSize:CGSizeMake(400, 200) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15] ,NSParagraphStyleAttributeName:paragraphStyle} context:nil].size;
    return size.width;
}




+(void)NotifyString:(NSString *)string Time:(int)Time
{
    
//   UIWindow * window = [UIApplication sharedApplication].windows[0];
    UIWindow * window =  [ZXZUtil getKeyWindow];

    UIViewController *rootVC = window.rootViewController;
    
    NotifyLabel *Label=[NotifyLabel new];
    
    
    NSInteger y1=(ScreenHeight-60)/2;
//    NSInteger w1=[Label calculatedwidthString:string]+50;
    NSInteger w1=200;

    NSInteger x1=(ScreenWidth-w1)/2;
    NSInteger h1=200;//60
    Label.frame = CGRectMake(x1,y1,w1, h1);
    Label.backgroundColor=[UIColor colorWithRed:59/255.0 green:59/255.0 blue:59/255.0 alpha:1];
    Label.alpha = 0.9;//0.8
    Label.layer.cornerRadius=10;
    Label.layer.masksToBounds=YES;
    Label.textColor=[UIColor redColor];
    Label.text=string;
    Label.textAlignment=NSTextAlignmentCenter;
    Label.font=[UIFont systemFontOfSize:18 weight:0];
    [rootVC.view addSubview:Label];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(Time * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
        [UIView animateWithDuration:0.5f animations:^{
            Label.alpha = 0;
        } completion:^(BOOL finished) {
            [Label removeFromSuperview];
        }];
        
        });
}


@end
